package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.RbacManagerDao;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.RbacManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RbacManagerServiceImpl implements RbacManagerService {
    //这个注解的required的意思：在编译期间不去检查有没有这个bean对象
    @Autowired(required = false)
    RbacManagerDao rbacManagerDao;
    @Override
    public ResponseEntity addOne(RbacManager rbacManager) {
        int i = rbacManagerDao.addOne(rbacManager);
        if (i>0){
            return ResponseEntity.SUCCESSFUL;
        }else {
            return ResponseEntity.FAIL;
        }
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        int i = rbacManagerDao.deleteOne(id);
        if (i>0){
            return new ResponseEntity(200,"删除成功",null);
        }else {
            return ResponseEntity.FAIL;
        }
    }

    @Override
    public ResponseEntity updateOne(RbacManager rbacManager) {
        int i = rbacManagerDao.updateOne(rbacManager);
        if (i>0){
            return ResponseEntity.SUCCESSFUL;
        }else {
            return ResponseEntity.FAIL;
        }
    }

    @Override
    public ResponseEntity findOne(int id) {
        RbacManager rbacManager = rbacManagerDao.findOne(id);
        return new ResponseEntity<RbacManager>(200,"查询成功",rbacManager);
    }


    @Override
    public ResponseEntity findAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<RbacManager> all = rbacManagerDao.findAll(currentPage,pageSize);
        PageInfo<RbacManager> pageInfo = PageInfo.of(all);
        return new ResponseEntity<>(200,"查询成功",pageInfo);
    }

    @Override
    public ResponseEntity findOneByAccountAndStatus(String account, String status) {
        List<RbacManager> all = rbacManagerDao.findOneByAccountAndStatus(account, status);
        return new ResponseEntity(200,"查询成功",all);
    }

    @Override
    @Cacheable(value = "rbacManager")
    public RbacManager login(String username, String password) {
        return rbacManagerDao.login(username,password);
    }

    @Override
    public ResponseEntity findUserByInput(RbacManager rbacManager) {
        return new ResponseEntity<>(200,"查询成功",rbacManagerDao.findUserByInput(rbacManager));
    }

    @Override
    public RbacManager findUser(String username) {
        System.out.println("finduser执行了");
        return rbacManagerDao.findUser(username);
    }
}
